package lion.framework.core.web.views;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import lion.framework.core.web.anno.ViewType;
import lion.framework.core.web.exception.WebException;
import lion.framework.core.web.path.ParameterizedPath;
import lion.framework.core.web.path.PathNode;
import lion.framework.core.web.path.PathNodeExecutor;
import lion.framework.core.web.path.PathNodeMapping;
import lion.framework.core.web.request.RequestModel;

/**
 * 内部重定向视图
 *
 * @author lion
 * @email hl_0074@sina.com
 * @date 2015年3月18日
 */
@ViewType("forward")
public class ForwardView implements IView {

	public static final String VIEW_TYPE = "forward";
	private String name;
	private String path;

	public ForwardView() {

	}

	public ForwardView(String path) {

		this.path = path;
	}

	@Override
	public void render(HttpServletRequest request, HttpServletResponse response) throws Exception {

		String path = ParameterizedPath.mapPath(this.path, new RequestModel(request));

		PathNode node = PathNodeMapping.find(path);
		if (node != null && node.getMethod() != null) {
			new PathNodeExecutor(node).execute(request, response);
		} else {
			throw new WebException(404, "File Not Found " + path);
		}
	}

	public void setPath(String path) {

		this.path = path;
	}

	@Override
	public String getName() {

		return this.name;
	}

	@Override
	public String type() {

		return VIEW_TYPE;
	}

}
